#include "XString.h"
#include "graphics.h"

#import "Texture2D.h"

#import <UIKit/UIKit.h>

CXString::CXString( void )
{
	m_pText = [[NSMutableString alloc] initWithString:@""];
	m_pTexture = NULL;
}

CXString::~CXString( void )
{
	[(NSMutableString *)m_pText release];
	[(Texture2D *)m_pTexture release];
}

CXString::CXString( const CXString& text )
{
	this->m_pText = [[NSMutableString alloc] initWithString:@""];
	this->m_pTexture = NULL;
	this->Copy(text);
}

CXString::CXString( const char * text )
{
	this->m_pText = [[NSMutableString alloc] initWithString:@""];
	this->m_pTexture = NULL;
	this->Copy(text);
}

const CXString & CXString::operator=( const CXString & text )
{
	this->Copy(text);
	return *this;
}

const CXString & CXString::operator=( const char * text )
{
	this->Copy(text);
	return *this;
}

CXString CXString::operator+( const CXString & text )
{
	CXString string = *this;
	string.Append(text);
	return string;
}

CXString CXString::operator+( const char * text )
{
	CXString string = *this;
	string.Append(text);
	return string;
}

void CXString::Format(const char *szFormat, ...)
{
	va_list ap;
	
	va_start(ap, szFormat);
	
	NSString *pstr = [[NSString alloc] initWithUTF8String:szFormat];
	NSString *pstring = [[NSString alloc] initWithFormat:pstr arguments:ap];

	[(NSMutableString *)m_pText setString:pstring];
	[pstr release];
	[pstring release];
	
	va_end(ap);
	
	ClearTexture();
}

void CXString::ClearTexture()
{
	if(m_pTexture)
	{
		[(Texture2D *)m_pTexture release];
		m_pTexture = NULL;
	}	
}

void CXString::Copy(const char * text)
{	
	NSString *pstr = [[NSString alloc] initWithUTF8String:text];
	[(NSMutableString *)m_pText setString:pstr];
	[pstr release];
	
	ClearTexture();
}

void CXString::Copy(const CXString& text)
{
	NSString *pstr = [[NSString alloc] initWithString:(NSString *)text.m_pText];
	[(NSMutableString *)m_pText setString:pstr];
	[pstr release];
	
	ClearTexture();
}

void CXString::CopyNum(int num)
{
	NSString *pstr = [[NSString alloc] initWithFormat:@"%d",num];
	[(NSMutableString *)m_pText setString:pstr];
	[pstr release];
	
	ClearTexture();
}

void CXString::CopyNum(double num)
{
	NSString *pstr = [[NSString alloc] initWithFormat:@"%.2f",num];
	[(NSMutableString *)m_pText setString:pstr];
	[pstr release];
	
	ClearTexture();
}

void CXString::Append(const char * text)
{
	NSString *pstr = [[NSString alloc] initWithUTF8String:text];
	[(NSMutableString *)m_pText appendString:pstr];
	[pstr release];
	
	ClearTexture();
}

void CXString::Append(const CXString& text)
{
	NSString *pstr = [[NSString alloc] initWithString:(NSString *)text.m_pText];
	[(NSMutableString *)m_pText appendString:pstr];
	[pstr release];
	
	ClearTexture();
}

void CXString::AppendNum(int num)
{
	NSString *pstr = [[NSString alloc] initWithFormat:@"%d",num];
	[(NSMutableString *)m_pText appendString:pstr];
	[pstr release];
	
	ClearTexture();
}

void CXString::AppendNum(double num)
{
	NSString *pstr = [[NSString alloc] initWithFormat:@"%2f",num];
	[(NSMutableString *)m_pText appendString:pstr];
	[pstr release];
	
	ClearTexture();
}

void CXString::Draw2D(int16 x, int16 y, const char * fontName, float fontSize, RGBVAL color, int16 width, int16 height, int16 mode)
{
	if(GetLength() == 0)return;
	
	if(!m_pTexture || m_width != width || m_height != height || m_FontSize != fontSize)
	{
		m_width  = width;
		m_height = height;
        m_FontSize = fontSize;
		ClearTexture();
		
		NSString * pFontName = [[NSString alloc] initWithCString:fontName];
		m_pTexture = [[Texture2D alloc] initWithString:(NSString *)m_pText dimensions:CGSizeMake(width,height) fontName:pFontName fontSize:fontSize drawMode:mode];
		[pFontName release];
	}
	
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glColor4ub(RGB_GET_RED(color),RGB_GET_GREEN(color),RGB_GET_BLUE(color),255);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
	//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	[(Texture2D *)m_pTexture drawAtPoint2D:CGPointMake( x, y ) rotate:0 scalex:1 scaley:1 region:CGRectMake(0,0,0,0)];	
	//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}

void CXString::Draw2D(int16 x, int16 y, TXT_ALIGN align, const char * fontName, float fontSize, RGBVAL color)
{
	if(align == TXT_CENTER)
		x -= GetTxtPixWidth(fontName,fontSize)>>1;
	else if(align == TXT_RIGHT)
		x -= GetTxtPixWidth(fontName,fontSize);
	
	Draw2D(x, y, fontName, fontSize, color);
}

bool CXString::Compare(const char * text)
{
	NSString *pstr = [[NSString alloc] initWithUTF8String:text];
	bool result = [(NSString *)m_pText isEqualToString:pstr];
	[pstr release];
	return result;
}

bool CXString::Compare(const CXString& text)
{
	return [(NSString *)m_pText isEqualToString:(NSString *)text.m_pText];
}

double CXString::GetValue()
{
	return [(NSString *)m_pText doubleValue];
}

CXString CXString::SubString(int16 location,int16 length)
{
	NSRange range;
	range.location = location;
	range.length = length;
	return [[(NSString *)m_pText substringWithRange:range] UTF8String];
}

const char * CXString::Utf8String()
{
	return [(NSString *)m_pText UTF8String];
}

void CXString::UpperCase()
{
	[(NSMutableString *)m_pText setString:[(NSString *)m_pText uppercaseString]];
	
	ClearTexture();
}

void CXString::LowerCase()
{
	[(NSMutableString *)m_pText setString:[(NSString *)m_pText lowercaseString]];
	
	ClearTexture();
}

void CXString::Split(CXList<CXString> *list,CXString param)
{
	NSArray *splist = [(NSMutableString *)m_pText componentsSeparatedByString:(NSString *)param.GetNSString()];
	int spNum = [splist count];
	for (int i = 0; i < spNum; i++) 
	{
		CXString *sptxt = new CXString();
		sptxt->Copy([[splist objectAtIndex:i] UTF8String]);
		list->AddData(sptxt);
	}
}

int CXString::Find(CXString param)
{
	NSRange range = [(NSMutableString *)m_pText rangeOfString:(NSString *)param.GetNSString()];
	if(range.length == 0)return -1;
	return range.location;
}

void CXString::Insert(CXString text,int pos)
{
	[(NSMutableString *)m_pText insertString:(NSString*)text.GetNSString() atIndex:pos];
}

void CXString::Replace(CXString find,CXString replace)
{
	NSRange range;
	while(1)
	{
		range = [(NSMutableString *)m_pText rangeOfString:(NSString *)find.GetNSString()];
		if(range.length > 0)[(NSMutableString *)m_pText replaceCharactersInRange:range withString:(NSString *)replace.GetNSString()];
		else break;
	};
}

int16 CXString::GetLength( void ) const
{
	return [(NSString *)m_pText length];
}

int16 CXString::GetTxtPixWidth(const char * fontName, float fontSize)
{
	NSString * pFontName = [[NSString alloc] initWithCString:fontName];
	UIFont * font = [UIFont fontWithName:pFontName size:fontSize];
	int16 result = [(NSString *)m_pText sizeWithFont:font].width;
	[pFontName release];
	return result;
}

int16 CXString::GetTxtPixHeight(const char * fontName, float fontSize)
{
	NSString * pFontName = [[NSString alloc] initWithCString:fontName];
	UIFont * font = [UIFont fontWithName:pFontName size:fontSize];
	int16 result = [(NSString *)m_pText sizeWithFont:font].height;
	[pFontName release];
	return result;
}

int16 CXString::GetTxtPixHeight(const char * fontName, float fontSize,int16 width,int16 height,int16 mode)
{
	NSString * pFontName = [[NSString alloc] initWithCString:fontName];
	UIFont * font = [UIFont fontWithName:pFontName size:fontSize];
	int16 result = [(NSString *)m_pText sizeWithFont:font constrainedToSize:CGSizeMake(width,height) lineBreakMode:(UILineBreakMode)mode].height;
	[pFontName release];
	return result;
}

